package es.cazadoresmadridejos.beans.controller.advertisement;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.displaytag.pagination.PaginatedList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import es.cazadoresmadridejos.beans.manager.advertisement.AdvertisementManager;
import es.cazadoresmadridejos.beans.manager.session.UserPreferences;
import es.cazadoresmadridejos.model.Advertisement.AdvertisementType;
import es.cazadoresmadridejos.model.Advertisement.OfferType;
import es.cazadoresmadridejos.objs.criteria.AdvertisementCriteria;
import es.cazadoresmadridejos.objs.web.Page;
import es.cazadoresmadridejos.util.constants.Constants;
import es.cazadoresmadridejos.web.util.ListUtil;


@SessionAttributes({"advertisement"})
@Controller
public class AdvertisementListController {
	
	@Autowired(required=false) private UserPreferences userPreferences;
	@Autowired private AdvertisementManager advertisementManager;

	@InitBinder
	public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Throwable{
		
		binder.registerCustomEditor(Date.class, "startDate",new CustomDateEditor(Constants.sdf,true));
		binder.registerCustomEditor(Date.class, "endDate",new CustomDateEditor(Constants.sdf,true));
	}

	
	@RequestMapping("/advertisementList.html")
	public ModelAndView list(HttpServletRequest request){
		ModelAndView mv = new ModelAndView();
		mv.addObject("advertisement", new AdvertisementCriteria());
		mv.addObject("urlTable", "advertisementListTable");

		listed(request, mv, new AdvertisementCriteria());
		
		return mv;
	}
	@RequestMapping("/advertisementListTable.html")
	public ModelAndView listTable(HttpServletRequest request,
		@ModelAttribute("advertisement")AdvertisementCriteria advertisementCriteria){
		ModelAndView mv = new ModelAndView();
		mv.addObject("advertisement", new AdvertisementCriteria());
		mv.addObject("urlTable", "advertisementListTable");

		listed(request, mv, advertisementCriteria);
		return mv;
	}
	
	
	
	@RequestMapping("/myAdvertisementList.html")
	public ModelAndView myList(
			HttpServletRequest request){
		ModelAndView mv = new ModelAndView();
		AdvertisementCriteria advertisementCriteria = new AdvertisementCriteria();
		advertisementCriteria.setPartnerId(userPreferences.getPartner().getId());
		mv.addObject("urlTable", "myAdvertisementListTable");
		
		mv.addObject("advertisement", advertisementCriteria);
		
		listed(request, mv, advertisementCriteria);
		
		return mv;
	}
	private void listed(HttpServletRequest request, ModelAndView mv,
			AdvertisementCriteria advertisementCriteria) {
		
		Page page = new Page(request);
		
		PaginatedList paginatedList = advertisementManager.findAllPaginated(page, advertisementCriteria);
		mv.addObject("list", paginatedList);
		mv.addObject("advertisementTypes",ListUtil.toString(AdvertisementType.values()));
		mv.addObject("offerTypes",ListUtil.toString(OfferType.values()));
		mv.addObject("type","create");
	}
	
	@RequestMapping("/myAdvertisementListTable.html")
	public ModelAndView myListTable(
			@ModelAttribute("advertisementCriteria")AdvertisementCriteria advertisementCriteria,
			HttpServletRequest request){
		ModelAndView mv = new ModelAndView();
		mv.addObject("urlTable", "myAdvertisementListTable");

		mv.addObject("advertisementCriteria", new AdvertisementCriteria());
		advertisementCriteria.setPartnerId(userPreferences.getPartner().getId());
		
		listed(request, mv, advertisementCriteria);
		
		return mv;
	}
	
	@RequestMapping("/advertisementDelete.html")
	public ModelAndView delete(
			@RequestParam("id")Long advertisementeId,
			HttpServletRequest request){
		ModelAndView mv = new ModelAndView("redirect:index.html");
		
		advertisementManager.delete(advertisementeId);
		
		mv.addObject("screen", "advertisementList.html");
		mv.addObject("okMessage", "THE_ADVERTISEMENT_WAS_DELETE_SUCCESSFULLY");
		
		return mv;
	}
}